/*
 * Copyright 2009 XueSong Guo.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cn.webwheel.database.test.domain;

import cn.webwheel.database.annotations.Select;
import cn.webwheel.database.annotations.Update;

import java.util.List;

abstract public class Board {

    private String id;
    private String name;
    private List<Thread> threads;
    private List<User> managers;
    private BoardGroup boardGroup;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<Thread> getThreads() {
        return threads;
    }

    public void setThreads(List<Thread> threads) {
        this.threads = threads;
    }

    public List<User> getManagers() {
        return managers;
    }

    public void setManagers(List<User> managers) {
        this.managers = managers;
    }

    public BoardGroup getBoardGroup() {
        return boardGroup;
    }

    public void setBoardGroup(BoardGroup boardGroup) {
        this.boardGroup = boardGroup;
    }

    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Board board = (Board) o;

        if (id != null ? !id.equals(board.id) : board.id != null) return false;

        return true;
    }

    public int hashCode() {
        return (id != null ? id.hashCode() : 0);
    }

    @Select(value = "select * from thread left outer join post on thread.id=post.thread left outer join user on post.author=user.id where board=#this.id# order by id limit #2# offset #1#",
    result = "*posts *posts.author")
    abstract public List<Thread> listThread(int skip, int limit) throws Exception;

    @Select("select * from thread")
    abstract public List<Thread> listAllThread() throws Exception;

    @Update("insert into userboard values(#1#, #this.id#)")
    abstract public void manageBy(String user) throws Exception;
}
